const Path = require('path');
const fs = require('fs');
let path = "C:/Users/gxr/Documents/Tencent Files/2065104337/Image/Group2"
let toPath = "D:/mirai/magia-mcl/data/net.mamoe.mirai-api-http/images/tempImg"

//是否是文件夹
if (!fs.existsSync(toPath)) {
  fs.mkdirSync(toPath)
}

let imgList: Array<Object>=[]

function getSpace(file: string) {
  //读取文件夹下内容
  let files = fs.readdirSync(file);
  //遍历文件
  files.forEach(async function (item: any, index: any) {
    await walk(file, item)
  });


  function walk(file: string, filename: string) {
    let states = fs.statSync(file + '/' + filename);
    //是否是文件夹
    if (states.isDirectory()) {
      getSpace(file + '/' + filename);
    } else {
      let imgToPath={'sourceFile':Path.join(file, filename),'destPath':Path.join(toPath, filename)}
      imgList.push(imgToPath)
    }
  }
}

function moveImg(imgObj){
  return new Promise((resolve, reject) => {
   //是文件进行移动
   

   var readStream = fs.createReadStream(imgObj.sourceFile);
   var writeStream = fs.createWriteStream(imgObj.destPath);
   readStream.pipe(writeStream);
   console.log('jin')
   readStream.on('close', ()=> { 
     console.log("移动完成")
     resolve(true)
   })
  })
}

async function go(){
  for(let imgObj of imgList){
    await moveImg(imgObj)
  }
}
getSpace(path);
go();


